﻿using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using DAL;
using Common;


public partial class Business_Business_NotFinish : System.Web.UI.Page
{
    protected int CurPage;

    protected int PageSize = Convert.ToInt32(ConfigurationSettings.AppSettings["PageSize"]);

    protected string Type1 = "1";

    protected string Type2 = "2";
    
    protected string Type3 = "4";

    protected string Type4 = "5";

    protected string MenuType;

    protected string Query;

    protected int UserType;


    protected void Page_PreRender(object sender, EventArgs e)
    {
        CurPage = string.IsNullOrEmpty(Request["curpage"]) ? 1 : Convert.ToInt32(Request["curpage"]);
        MenuType = string.IsNullOrEmpty(Request["menutype"]) ? "" : Request["menutype"].ToString();
        Query = string.IsNullOrEmpty(Request["Query"]) ? "all" : Request["Query"].ToString();
        UserType = Convert.ToInt32(Session["UserType"]);

        if (!this.IsPostBack)
        {
            List<NewsType> NewsTypeList = new Sys_Para().SelectNewsType();
            foreach (NewsType nt in NewsTypeList)
                ClsID.Items.Add(new ListItem("┣ " + nt.TypeName, nt.TypeKey.ToString()));
        }


            OneDate.Attributes.Add("onclick", "selectDate(this)");
            TwoDate.Attributes.Add("onclick", "selectDate(this)");

            NewsDataBind(Lv_News1,CreateHashTable(Type1));
            NewsDataBind(Lv_News2, CreateHashTable(Type2));
            NewsDataBind(Lv_News3, CreateHashTable(Type3));
            NewsDataBind(Lv_News4, CreateHashTable(Type4));
    }

    private void NewsDataBind(ListView Lv,Hashtable QueryTable)
    {
        string URL = "Business.aspx?menutype=" + MenuType;

        Lv.DataSource = new News().SelectNews(QueryTable, CurPage);
        Lv.DataBind();
    }

    private Hashtable CreateHashTable(string Type)
    {
        Hashtable tb = new Hashtable();
        bool QueryAdd = false;

        if (Query != "")
        {
            tb.Add("Query", Query);
            QueryAdd = true;
        }

        if (MenuType != "")
        {
            tb.Add("MenuType", MenuType);
            QueryAdd = true;
        }

        if (Type != "")     //菜单传入的任务类型
        {
            tb.Add("Type", Type);
            QueryAdd = true;
        }

        if (!string.IsNullOrEmpty(ClsID.Text))
        {
            tb.Add("ClsID", ClsID.Text);
            QueryAdd = true;
        }
        if (!string.IsNullOrEmpty(txt_Type.Text))   //查询传入的任务类型
        {
            tb.Add("txt_Type", txt_Type.Text);
            QueryAdd = true;
        }
        if (!string.IsNullOrEmpty(Keyword.Text.Trim()))
        {
            tb.Add("Keyword", Keyword.Text.Trim());
            QueryAdd = true;
        }
        if (!string.IsNullOrEmpty(OneDate.Text.Trim()))
        {
            tb.Add("OneDate", OneDate.Text.Trim());
            QueryAdd = true;
        }
        if (!string.IsNullOrEmpty(TwoDate.Text.Trim()))
        {
            tb.Add("TwoDate", TwoDate.Text.Trim());
            QueryAdd = true;
        }
        tb = QueryAdd ? tb : null;

        return tb;
    }

    //搜索
    protected void submit_Click(object sender, EventArgs e)
    {
        CurPage = 1;

        NewsDataBind(Lv_News1, CreateHashTable(Type1));
        NewsDataBind(Lv_News2, CreateHashTable(Type2));
        NewsDataBind(Lv_News3, CreateHashTable(Type3));
        NewsDataBind(Lv_News4, CreateHashTable(Type4));
    }

    //Lv_News1
    protected void Lv_News1_ItemCommand(object source, ListViewCommandEventArgs e)
    {
        int NewsKey = Convert.ToInt32(e.CommandArgument);

        if (e.CommandName == "del")
        {
            new News().DelNews(NewsKey);
        }

        CurPage = 1;
        NewsDataBind(Lv_News1, CreateHashTable(Type1));
    }

    protected void Lv_News1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            if (UserType == 2 || UserType == 4)
                ((LinkButton)e.Item.FindControl("del")).Visible = Account.ShowForUserTypeList1("24", UserType);
            else
                ((LinkButton)e.Item.FindControl("del")).Visible = false;

        }
    }

    //Lv_News2
    protected void Lv_News2_ItemCommand(object source, ListViewCommandEventArgs e)
    {
        int NewsKey = Convert.ToInt32(e.CommandArgument);

        if (e.CommandName == "del")
        {
            new News().DelNews(NewsKey);
        }

        CurPage = 1;
        NewsDataBind(Lv_News2, CreateHashTable(Type2));
    }

    protected void Lv_News2_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            if (UserType == 2 || UserType == 4)
                ((LinkButton)e.Item.FindControl("del")).Visible = Account.ShowForUserTypeList1("24", UserType);
            else
                ((LinkButton)e.Item.FindControl("del")).Visible = false;

        }
    }

    //Lv_News3
    protected void Lv_News3_ItemCommand(object source, ListViewCommandEventArgs e)
    {
        int NewsKey = Convert.ToInt32(e.CommandArgument);

        if (e.CommandName == "del")
        {
            new News().DelNews(NewsKey);
        }

        CurPage = 1;
        NewsDataBind(Lv_News3, CreateHashTable(Type3));
    }

    protected void Lv_News3_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            if (UserType == 2 || UserType == 4)
                ((LinkButton)e.Item.FindControl("del")).Visible = Account.ShowForUserTypeList1("24", UserType);
            else
                ((LinkButton)e.Item.FindControl("del")).Visible = false;

        }
    }

    //Lv_News4
    protected void Lv_News4_ItemCommand(object source, ListViewCommandEventArgs e)
    {
        int NewsKey = Convert.ToInt32(e.CommandArgument);

        if (e.CommandName == "del")
        {
            new News().DelNews(NewsKey);
        }

        CurPage = 1;
        NewsDataBind(Lv_News4, CreateHashTable(Type3));
    }

    protected void Lv_News4_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            if (UserType == 2 || UserType == 4)
                ((LinkButton)e.Item.FindControl("del")).Visible = Account.ShowForUserTypeList1("24", UserType);
            else
                ((LinkButton)e.Item.FindControl("del")).Visible = false;

        }
    }


    protected string GetUserRealName(int UserKey)
    {
        Sys_Account account = new Account().GetAccountByKey(Convert.ToInt32(Eval("UserKey")));
        return (account == null) ? "" : account.RealName;
    }

    protected string GetQuery(string Query)
    {   
        string query = "";
        switch(Query)
        {
            case "all":
                query = "全部";
                break;
            case "day":
                query = "当天";
                break;
            case "month":
                query = "当月";
                break;
        }
        return query;
    }
}
